﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ComputerVision.Data;
using ComputerVision.Data.Repositories;

namespace ComputerVision.Services
{
    public class NguyenNhanChayService : IService<NguyenNhanChay>
    {
        private readonly IEntityRepository<NguyenNhanChay> _nguyenNhanChayRepository;
        public NguyenNhanChayService(IEntityRepository<NguyenNhanChay> nguyenNhanChayRepository)
        {
            _nguyenNhanChayRepository = nguyenNhanChayRepository;
        }

        public NguyenNhanChay Create(NguyenNhanChay t)
        {
            try
            {
                if (t == null)
                    throw new ArgumentNullException("entity");
                _nguyenNhanChayRepository.InsertOnCommit(t);
                _nguyenNhanChayRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage) + Environment.NewLine;

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
            return t;
        }

        public NguyenNhanChay Update(NguyenNhanChay t)
        {
            NguyenNhanChay item;
            try
            {
                item = _nguyenNhanChayRepository.Get(t.Id);
                item.NguyenNhan = t.NguyenNhan;
                //item.Created = DateTime.Now;
                //item.CreatedBy_Id = t.CreatedBy_Id;
                _nguyenNhanChayRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += Environment.NewLine +
                               string.Format("Property: {0} Error: {1}", validationError.PropertyName,
                                   validationError.ErrorMessage);

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
            return item;
        }

        public NguyenNhanChay Detail(Guid id)
        {
            var item = _nguyenNhanChayRepository.Get(id);
            return item;
        }

        public List<NguyenNhanChay> ToList()
        {
            var item = _nguyenNhanChayRepository.GetAll().ToList();
            return item;
        }

        public void Delete(NguyenNhanChay t)
        {
            try
            {
                if (t == null)
                    throw new ArgumentNullException("entity");
                _nguyenNhanChayRepository.DeleteOnCommit(t);
                _nguyenNhanChayRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;

                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += Environment.NewLine + string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
        }
    }
}
